package com.qianyu.api.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianyu.core.entity.base.Result;
import com.qianyu.core.entity.bean.QqAccount;
import com.qianyu.core.entity.bean.User;
import com.qianyu.core.entity.dto.QqAccountDto;
import com.qianyu.core.entity.vo.QqAccountVo;
import com.qianyu.core.service.QqAccountService;
import com.qianyu.core.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "前台管理-账号管理", description = "wangzhicheng")
@RestController(value = "FrontQqAccountController")
@RequestMapping("/front/qq")
@RequiredArgsConstructor
public class QqAccountController {
    private final QqAccountService qqAccountService;

    private final UserService userService;

    /**
     * 服务商查询代理账号列表
     */
    @PostMapping("/pageByAgency")
    @ApiOperation(value = "服务商查询代理账号列表")
    Result<Page<QqAccountVo.AccountSimpleVo>> pageByAgency(@RequestBody @Validated QqAccountDto.QqAccountSimplePageDto dto) {
        Page<QqAccountVo.AccountSimpleVo> page = qqAccountService.pageByAgency(dto);
        Map<Long, String> map = userService.list().stream().collect(Collectors.toMap(User::getId, User::getNickname));
        page.getRecords().forEach(q -> q.setNickname(map.get(q.getUserId())));
        return Result.success(page);
    }

    /**
     * 操作员获取账号列表
     */
    @PostMapping("/pageByRent")
    @ApiOperation(value = "操作员获取账号列表")
    Result<Page<QqAccount>> pageByRent(@RequestBody @Validated QqAccountDto.QqAccountRentPageDto dto){
        return Result.success(qqAccountService.pageByRent(dto));
    }


    /**
     * 分配账号
     */
    @PostMapping("/distributeAccount")
    @ApiOperation(value = "分配账号")
    Result<String> distributeAccount(@RequestBody @Validated QqAccountDto.QqAccountDistributeDto dto) {
        qqAccountService.distributeAccount(dto);
        return Result.success();
    }

}
